package cn.szabadsag.hrm.service.impl;

import cn.szabadsag.hrm.client.SearchServiceClient;
import cn.szabadsag.hrm.constants.RedisConstants;
import cn.szabadsag.hrm.context.LoginContext;
import cn.szabadsag.hrm.doc.CourseDoc;
import cn.szabadsag.hrm.domain.Course;
import cn.szabadsag.hrm.domain.CourseDetail;
import cn.szabadsag.hrm.domain.CourseMarket;
import cn.szabadsag.hrm.domain.LoginUser;
import cn.szabadsag.hrm.dto.CourseAddParamDTO;
import cn.szabadsag.hrm.exception.Assert;
import cn.szabadsag.hrm.exception.BusinessException;
import cn.szabadsag.hrm.mapper.CourseMapper;
import cn.szabadsag.hrm.service.ICourseDetailService;
import cn.szabadsag.hrm.service.ICourseMarketService;
import cn.szabadsag.hrm.service.ICourseService;
import cn.szabadsag.hrm.util.AjaxResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangcong
 * @since 2022-04-10
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseDetailService courseDetailService;

    @Autowired
    private ICourseMarketService courseMarketService;

    @Autowired
    private SearchServiceClient searchServiceClient;

    /**
     * 新增
     * @param courseAddParamDTO：传递的参数
     */
    @Override
    @Transactional
    public void add(CourseAddParamDTO courseAddParamDTO) {
        //  1.参数校验 JSR303参数校验方法
        Course course = courseAddParamDTO.getCourse();
        CourseMarket courseMarket = courseAddParamDTO.getCourseMarket();
        CourseDetail courseDetail = courseAddParamDTO.getCourseDetail();

        //  2.条件判断：比如说，课程名不能重复
        String courseName = course.getName();

        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.eq("name",courseName);
        Course selectOne = selectOne(wrapper);
        Assert.assertNull(selectOne,"课程名重复！");

        LoginUser login = LoginContext.getLogin();
        //  3.保存课程
        course.setStatus(RedisConstants.COURSE_OFFLINE);//  0表示未发布
        course.setTenantId(login.getTenantId());
        course.setTenantName(login.getTenantName());
        course.setUserId(login.getId());
        course.setUserName(login.getUsername());
        insert(course);

        //  4.将课程的id设置到课程营销表的id 保存课程营销表
        courseMarket.setId(course.getId());
        courseMarketService.insert(courseMarket);


        //  5.将课程的id设置到课程详情表的id 保存课程详情表
        courseDetail.setId(course.getId());
        courseDetailService.insert(courseDetail);

    }

    /**
     * 课程上架，并同步到ES
     * @param courseId
     */
    @Override
    @Transactional
    public void onLineCourse(Long courseId) {
        //  1.参数判断
        Assert.assertNotNull(courseId,"非法请求！");
        //  2.条件判断
        Course course = selectById(courseId);
        Assert.assertNotNull(course,"非法请求！");
        if (course.getStatus().intValue() == RedisConstants.COURSE_ONLINE){
            throw new BusinessException("该课程不处于待上线状态！");
        }
        //  3.修改为上线状态
        course.setStatus(RedisConstants.COURSE_ONLINE);
        updateById(course);

        //  4.同步到ES
        CourseMarket courseMarket = courseMarketService.selectById(courseId);

        CourseDoc doc = new CourseDoc();
        doc.setId(course.getId());
        BeanUtils.copyProperties(course,doc);
        BeanUtils.copyProperties(courseMarket,doc);
        doc.setChargeName(courseMarket.getCharge() == 1 ? "收费":"免费");
        doc.setOnlineTime(new Date());
        AjaxResult ajaxResult = searchServiceClient.add(doc);
        System.out.println("添加后的结果:" + ajaxResult.isSuccess());
        Assert.assertTrue(ajaxResult.isSuccess(),"课程上架失败！");
    }

    /**
     * 课程下架，并且调用ES的服务删除ES中的课程
     * @param id
     */
    @Override
    @Transactional
    public void offLineCourse(Long id) {
        //  1.参数判断
        Assert.assertNotNull("id","非法请求！");
        //  2.条件判断
        Course course = selectById(id);
        Assert.assertNotNull("course","该课程不存在！");
        //  3.修改数据库中课程的状态为0
        course.setStatus(0);
        updateById(course);
        //  4.删除ES中对应的课程信息
        searchServiceClient.delete(course.getId());
    }
}
